
const { Configuration } = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader')
const Mock = require('mockjs')

/** 
* @type {Configuration}
*/
const config = {
    // development 本地
    // production  生产/线上
    mode: 'development',
    // 入口
    entry: './src/index.js',
    // 出口
    output: {
        // 文件名
        filename: '[name].[contenthash].bundle.js',
        // 路径
        path: path.resolve(__dirname, './dist'),
        clean: true, // 在生成文件之前清空 output 目录
    },
    module: {
        rules: [
            {
                test: /\.s?css$/i,
                use: ['vue-style-loader', 'css-loader', 'sass-loader'],
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                type: 'asset',
            },
            {
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                    },
                },
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
          
        ],
    },    
    resolve: {
        extensions: ['.js', '.vue'],
        alias: {
            "@": path.resolve(__dirname, './src')
        }
    },
    plugins: [
        new HtmlWebpackPlugin(
            {
                template: './public/index.html'
            }
        ),
        new VueLoaderPlugin()
    ],
    devServer: {
        // 配置端口号
        port: 3000,
        // 服务器地址
        host: 'localhost',
        // 自动打开浏览器
        open: true,
        // 热更新：自动刷新
        hot: true,
        // 配置接口
        setupMiddlewares(middlewares, devServer){
            // 模拟数据
            const { list, todoList } = Mock.mock({
                'list|10': [{
                    'id': '@id',
                    'title': '@ctitle',
                    'desc': '@cword(20, 50)',
                    'img': '@image(100x100, @color)'
                }],
                "todoList|10": [{
                    'id': '@id',
                    'title': '@ctitle',
                    "state": false
                }]
            })
            // 模拟接口
            devServer.app.get('/api/list', (req, res) =>{
                res.send({
                    code: 200,
                    data: list,
                    message: 'ok'
                })
            })
              // 模拟接口
              devServer.app.get('/api/todoList', (req, res) =>{
                res.send({
                    code: 200,
                    data: todoList,
                    message: 'ok'
                })
            })

            return middlewares
        }
    }
}

module.exports = config
